package com.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.web.multipart.MultipartFile;

import sun.misc.BASE64Decoder;
/**
 * 
*    
* @项目名称：sqt   
* @类名称：BASE64DecodedMultipartFile   
* @类描述：   base64转file
* @开发者：gml   
* @创建时间：2019-2-20 上午10:47:04   
* @version
 */
public class BASE64DecodedMultipartFile implements MultipartFile {

    private final byte[] imgContent;
    private final String header;

    public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
        this.imgContent = imgContent;
        this.header = header.split(";")[0];
    }

    @Override
    public String getName() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
    }

    @Override
    public String getOriginalFilename() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + (int)Math.random() * 10000 + "." + header.split("/")[1];
    }

    @Override
    public String getContentType() {
        // TODO - implementation depends on your requirements
        return header.split(":")[1];
    }

    @Override
    public boolean isEmpty() {
        return imgContent == null || imgContent.length == 0;
    }

    @Override
    public long getSize() {
        return imgContent.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return imgContent;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(imgContent);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
    	try{
    		 OutputStream out = new FileOutputStream(dest);    
             out.write(imgContent);    
             out.flush();    
             out.close(); 
    	}catch(IOException e){
    		e.printStackTrace();
    	}
    }
    /**
     * 
     * @方法名：base64ToMultipart
     * @描述:   base64转file
     * @参数: @param base64
     * @参数: @return
     * @返回类型: MultipartFile
     * @开发者： gml
     * @创建时间: 2019-2-20 上午10:54:37
     */
    public static MultipartFile base64ToMultipart(String base64) {
        try {
            String[] baseStrs = base64.split(",");

            BASE64Decoder decoder = new BASE64Decoder();
            byte[] b = new byte[0];
            b = decoder.decodeBuffer(baseStrs[1]);

            for(int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }

            return new BASE64DecodedMultipartFile(b, baseStrs[0]);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
  //对字节数组字符串进行Base64解码并生成图片
    public static String GenerateImage(String base64Img,String realPath,String imgTypes ){  
        String ret_fileName = null;
        // 临时文件路径              
        File file_normer = new File(realPath);  
        if (!file_normer.exists()) {  
            file_normer.mkdirs();  
        }  
         if (base64Img == null) // 图像数据为空    
                return "图像数据为空";    
         base64Img = base64Img.replaceAll("data:image/"+imgTypes+";base64,", "");    
            BASE64Decoder decoder = new BASE64Decoder();    
            try {    
                // Base64解码    
                byte[] b = decoder.decodeBuffer(base64Img);    
                for (int i = 0; i < b.length; ++i) {    
                    if (b[i] < 0) {// 调整异常数据    
                        b[i] += 256;    
                    }    
                }    
                // 生成jpeg图片       
                ret_fileName = System.currentTimeMillis()+"."+imgTypes+"";
                File file = new File(realPath + ret_fileName );  
                OutputStream out = new FileOutputStream(file);    
                out.write(b);    
                out.flush();    
                out.close();    
            } catch (Exception e) {    
                e.printStackTrace();    
            }    
            return ret_fileName ;   
           
       }
   
}
